﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;

namespace Archipel
{
    public class RandomImageGeneator
    {
        /// <summary>
        /// Generate a random numbers
        /// </summary>
        /// <param name="min">from value</param>
        /// <param name="max">to value</param>
        /// <returns></returns>
        public static int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }



        /// <summary>
        /// Generate a random string
        /// </summary>
        /// <param name="size">numbers of characters</param>
        /// <param name="lowerCase">is lowercase if true and otherwish</param>
        /// <returns></returns>
        public static string RandomString(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }



        /// <summary>
        /// Generate an image with a string
        /// </summary>
        /// <param name="text">text what you want to display on image</param>
        /// <returns></returns>
        public static Bitmap Generate(string text)
        {
            Bitmap bm = new Bitmap(1, 1);

            int intWidth = 0;
            int intHeight = 0;

            Font font = new Font("Tahoma", 16, FontStyle.Italic, GraphicsUnit.Pixel);

            Graphics g = Graphics.FromImage(bm);

            intWidth = (int)g.MeasureString(text, font).Width;
            intHeight = (int)g.MeasureString(text, font).Height;

            bm = new Bitmap(bm, new Size(intWidth, intHeight));

            g = Graphics.FromImage(bm);

            g.Clear(Color.WhiteSmoke);   //set background color

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;     //set quality of image

            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            SolidBrush b = new SolidBrush(Color.Black);

            g.DrawString(text, font, b, 0, 0);


            //save image as a pathname which map with source of confirmImage on Contact.aspx site
            bm.Save(@"D:\My Documents\Visual Studio 2008\Projects\Archipel\Archipel\Images\randomImg.jpg", ImageFormat.Jpeg);  //*** path name must be replace with FULLPATH of Images folder

            g.Flush();

            return bm;
        }
    }
}
